var flag = false;
var label = 'js-pushlet-net.html';
var controlReady = false;
var listenReady = false;

var nick = "testaccount";

var chatFrame;
var chatDoc;

function debug(msg) {

	if (!parent.p_debug) {
		//alert(msg);
		return;
	}

	parent.p_debug(flag, label, msg);
}

// Callback from pushed JavaScript
function push() {
	debug('push() called');

	if (!parent._push) {
		debug('parent._push() not available');
		return;
	}

	parent._push(push.arguments);
}

// Is everything loaded ?
function isLoaded() {
	return (controlReady == true && listenReady == true);
}

// Can control frame accept new URI ?
function isControlReady() {
	return controlReady == true;
}

// Can listen frame accept new URI ?
function isListenReady() {
	return listenReady == true;
}

function setControlURI(uri) {
	debug('setControlURI() uri=' + uri);
	controlReady = false;
	controlFrame.location.href = uri;
}

function listen(uri) {
	listenFrame.location.href = uri;
	debug('listen() called uri=' + uri);
}

// Signals that listen frame can accept next URI
function _listenFrameReady() {
	//	debug('_listenFrameReady() loaded ' + listenFrame.location.href );
	listenReady = true;
}

// Show network debug window
function p_setDebug(aFlag) {
	flag = aFlag;
}

// Event Callback for join
function onJoinAck(event) {
	appendMessage('Listening to chat');
}

// Event Callback: display all events
function onData(event) {
	var currentTimeStamp = App.Base.Common.func.getCurrentSysTimeStamp();
	p_debug(false, "pushlet-app", 'event received event=' + event.getEvent() );
	var action = event.get('action');
	var content = 'none action=' + action;
	 if (action == 'send') {
		content = '<font color="#3366ff">' + event.get('nick') + ' ' + currentTimeStamp + '</font color><br>' + event.get('msg');
	} else if (action == 'enter') {
		content = '<font color="red">系统消息' + currentTimeStamp + ' 欢迎 &lt;' + event.get('nick') + '&gt; 进入聊天室</font>';
	} else if (action == 'exit') {
		content = '<font color="red">系统消息' + currentTimeStamp + ' &lt;' + event.get('nick') + '&gt; 离开了聊天室</font>';
	}

	if (App.OnlineServices.OnlineChatRoom.RECEIVE_MSG_WINDOW_STATUS == App.Base.common.param.CHATROOM_RECEIVE_MSG_WINDOW_STATUS_OFF)
	{
		return;
	}
	appendMessage(content);
}

// Event Callback: display all events
function onNack(event) {
	alert('negative response from server: ' + event.getEvent() + ' reason: ' + event.get('p_reason'));
}

	function appendMessage (content){
		var newDiv = chatDoc.createElement("DIV");
		newDiv.innerHTML = content;
		chatDoc.getElementById("contents").appendChild(newDiv);
		chatFrame.scrollTo(0, chatDoc.getElementById("contents").offsetHeight);
	}

	function chat_init(n){
		var ifmId = document.getElementsByTagName("IFRAME")[0].id; //alert(ifmId);
		if (window.frames && window.frames[ifmId]) //IE 5 (Win/Mac), Konqueror, Safari
			chatFrame = window.frames[ifmId];
		else if (document.getElementById(ifmId).contentWindow) //IE 5.5+, Mozilla 0.9+, Opera
			chatFrame = document.getElementById(ifmId).contentWindow;
		else //Moz < 0.9 (Netscape 6.0)
			chatFrame = document.getElementById(ifmId);

		if(chatFrame.document) //Moz 0.9+, Konq, Safari, IE, Opera
			chatDoc = chatFrame.document;
		else //Moz < 0.9 (Netscape 6.0)
			chatDoc = chatFrame.contentDocument;

		enterChat(n);
	}

function enterChat(n){
	nick = n;
	//PL.pushletURL = 'http://localhost:8081/pushlet/pushlet.srv';
	p_join_listen('/chat');
	p_publish('/chat', 'action', 'enter', 'nick', nick);
}

function sendMsg(msg){
	p_publish('/chat', 'action', 'send', 'nick', nick, 'msg', msg);
}

function leaveChat(){
	// Send exit to chatters
	p_publish('/chat', 'action', 'exit', 'nick', nick);

	// Stop pushlet session
	p_leave();
}